<template>
    <div class="home-content">
        <div class="content-wrapper">
            <!-- 通知栏 -->
            <div class="notice-bar">
                <div class="notice-icon"></div>
                <div class="notice-text">{{ state.notice }}</div>
            </div>

            <!-- 主要内容区 -->
            <div class="main-content">
                <!-- 左侧地图区域 -->
                <div class="map-section" 
                     @mouseenter="stopAutoPlay"
                     @mouseleave="startAutoPlay">
                    <div class="map-container">
                        <img :src="state.mapImage"  class="map-image" alt="">
                        <div class="map-indicators">
                            <span v-for="(dot, index) in 1" :key="index"
                                :class="['indicator-dot', { active: currentMapIndex === index }]"
                                @click="currentMapIndex = index"></span>
                        </div>
                    </div>
                </div>

                <!-- 右侧新闻列表 -->
                <div class="news-section">
                    <ul class="news-list">
                        <li v-for="(news, index) in state.newsList" :key="index" @click="handleNewsClick(news)">
                            <span class="news-item-title">{{ news.title }}</span>
                            <span class="news-date">{{ news.date }}</span>
                        </li>
                    </ul>
                </div>
            </div>

            <!-- 专题专栏 -->
            <!-- <div class="special-topics">
        <div class="special-header">
          <h2>专题专栏</h2>
          <div class="blue-line"></div>
        </div>
        <div class="topics-container">
          <div class="topics-row">
            <div v-for="topic in state.topics" 
                 :key="topic.id" 
                 class="topic-item"
                 @click="handleTopicClick(topic)">
              <img :src="topic.imageUrl" :alt="topic.title">
            </div>
          </div>
        </div>
      </div> -->
        </div>
    </div>
</template>

<script setup>
import { ref, computed, onMounted, watch, onUnmounted } from 'vue';
import { getNewsBar, getNews } from '@/api';
import { getAssets } from '../utils/index';

// 状态管理
const state = ref({
    notice: '《国省一体化人工影响天气综合业务平台（天工）组件化技术指南》已发布！',
    mapImage: getAssets('image/carousel/6.png'),
    newsList: [
        { title: '中试平台是什么？中试平台和天工的关系是什么？', date: '2024-03-17' },
        { title: '什么是组件化？为什么要组件化改造？', date: '2024-03-17' },
        { title: '组件化如何业务化应用？实践案例分享', date: '2024-03-16' },
        // { title: '天工平台最新版本V2.0发布，新增多项功能', date: '2024-03-15' },
        // { title: '人工影响天气作业指挥系统升级公告', date: '2024-03-14' },
        // { title: '2024年度天气改造技术研讨会召开通知', date: '2024-03-13' },
        // { title: '关于开展天工平台用户满意度调查的通知', date: '2024-03-12' },
        // { title: '人工影响天气作业标准化流程指南发布', date: '2024-03-11' },
    ],
    topics: [
        { id: 1, imageUrl: getAssets('image/carousel/u21.png'), title: '专题1' },
        { id: 2, imageUrl: getAssets('image/carousel/u21.png'), title: '专题2' },
        { id: 3, imageUrl: getAssets('image/carousel/u21.png'), title: '专题3' },
        { id: 4, imageUrl: getAssets('image/carousel/u21.png'), title: '专题4' },
    ]
});

const currentMapIndex = ref(0);
const mapImages = [

];

// 自动轮播
let autoPlayTimer = null;

const startAutoPlay = () => {
    // autoPlayTimer = setInterval(() => {
    //     currentMapIndex.value = 1
    // }, 3000);
};

const stopAutoPlay = () => {
    if (autoPlayTimer) {
        clearInterval(autoPlayTimer);
    }
};

// 监听当前图片
watch(() => currentMapIndex.value, (newVal) => {
    state.value.mapImage = mapImages[newVal];
});

// 将专题数据分组，每组4个
const topicGroups = computed(() => {
    const groups = [];
    for (let i = 0; i < state.value.topics.length; i += 4) {
        groups.push(state.value.topics.slice(i, i + 4));
    }
    return groups;
});

// 获取数据
const fetchData = async () => {
    try {
        const [newsRes, newsBarRes] = await Promise.all([getNews(), getNewsBar()]);
        if (newsRes.code === 200) {
            // 处理新闻数据
        }
        if (newsBarRes.code === 200) {
            // 处理通知栏数据
        }
    } catch (error) {
        console.error('数据获取失败:', error);
    }
};

const handleNewsClick = (news) => {
    console.log('点击新闻:', news);
};

const handleTopicClick = (topic) => {
    console.log('点击专题:', topic);
};

onMounted(() => {
    fetchData();
});

</script>

<style lang="scss" scoped>
.home-content {
    width: 100%;
    background-color: #EAF7FF;
    padding: 20px 0;

    .content-wrapper {
        width: 70%;
        margin: 0 auto;
        background: #fff;
        padding: 12px;

        // 通知栏样式
        .notice-bar {
            display: flex;
            align-items: center;
            background: #E9F5FF;
            padding: 8px 15px;
            margin-bottom: 15px;

            .notice-icon {
                width: 70px;
                height: 23px;
                background: url('../assets/image/notice.png') center/contain no-repeat;
                background-size: 100% 100%;
                margin-right: 10px;
            }

            .notice-text {
                flex: 1;
                font-size: 14px;
                color: #333;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
        }

        // 主要内容区样式
        .main-content {
            display: flex;
            gap: 15px;
            margin-bottom: 15px;
            height: 460px;

            // 左侧地图区域
            .map-section {
                flex: 1;
                position: relative;
                background: #fff;
                border: 1px solid #E4E7ED;
                display: flex;
                flex-direction: column;

                .map-title {
                    padding: 10px 15px;
                    font-size: 16px;
                    color: #333;
                    border-bottom: 1px solid #E4E7ED;
                    font-weight: 500;
                    height: 42px;
                    display: flex;
                    align-items: center;
                }

                .map-container {
                    position: relative;
                    flex: 1;
                    overflow: hidden;

                    .map-image {
                        width: 100%;
                        height: 100%;
                        object-fit: contain;
                        padding: 10px;
                    }

                    .map-indicators {
                        position: absolute;
                        bottom: 15px;
                        left: 50%;
                        transform: translateX(-50%);
                        display: flex;
                        gap: 8px;
                        background: rgba(0, 0, 0, 0.3);
                        padding: 6px 12px;
                        border-radius: 12px;
                        z-index: 1;

                        .indicator-dot {
                            width: 8px;
                            height: 8px;
                            border-radius: 50%;
                            background: rgba(255, 255, 255, 0.5);
                            cursor: pointer;
                            transition: all 0.3s ease;

                            &.active {
                                background: #fff;
                                transform: scale(1.2);
                            }
                        }
                    }
                }
            }

            // 右侧新闻区域
            .news-section {
                width: 55%;
                background: #fff;
                border: 1px solid #E4E7ED;
                padding: 15px;
                display: flex;
                flex-direction: column;

                .news-content {
                    color: #666;
                    font-size: 14px;
                    line-height: 1.6;
                    margin-bottom: 15px;
                    padding: 10px;
                    background: #f8f9fa;
                    border-radius: 4px;
                    height: 80px;
                    overflow: hidden;
                }

                .news-list {
                    flex: 1;
                    overflow-y: auto;
                    padding-right: 5px;

                    &::-webkit-scrollbar {
                        width: 4px;
                    }

                    &::-webkit-scrollbar-thumb {
                        background: #ccc;
                        border-radius: 2px;
                    }

                    li {
                        display: flex;
                        justify-content: space-between;
                        padding: 10px 0;
                        border-bottom: 1px dashed #E4E7ED;
                        cursor: pointer;
                        transition: all 0.3s ease;

                        &:hover {
                            color: #0080E7;
                            padding-left: 5px;
                        }

                        .news-item-title {
                            flex: 1;
                            padding-left: 12px;
                            position: relative;
                            white-space: nowrap;
                            overflow: hidden;
                            text-overflow: ellipsis;
                            font-size: 14px;

                            &::before {
                                content: '•';
                                position: absolute;
                                left: 0;
                                color: #666;
                            }
                        }

                        .news-date {
                            color: #999;
                            font-size: 12px;
                            margin-left: 10px;
                            min-width: 75px;
                            text-align: right;
                        }
                    }
                }
            }
        }

        // 专题专栏样式
        .special-topics {
            .special-header {
                position: relative;
                margin-bottom: 20px;

                h2 {
                    font-size: 18px;
                    font-weight: bold;
                    margin: 0;
                    padding-bottom: 8px;
                }

                .blue-line {
                    position: absolute;
                    left: 0;
                    bottom: 0;
                    width: 40px;
                    height: 2px;
                    background: #006CC3;
                }
            }

            .topics-container {
                padding: 0 10px;

                .topics-row {
                    display: flex;
                    justify-content: space-between;
                    gap: 15px;

                    .topic-item {
                        flex: 1;
                        cursor: pointer;

                        img {
                            width: 100%;
                            height: 120px;
                            object-fit: cover;
                        }

                        &:hover {
                            opacity: 0.9;
                        }
                    }
                }
            }
        }
    }
}
</style>